package xju_3_4.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import xju_3_4.entity.User;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

//    @RequestMapping(value = "/test/t",method = RequestMethod.GET)
    @GetMapping("/test/t")
    @ResponseBody
    public String test(@RequestParam(value = "name") String username,@RequestParam(value = "password",required = false,defaultValue = "111") String password) {
        System.out.println(username);
        System.out.println(password);
        return username+":"+password;
    }

    @RequestMapping("/test1/**")
    public String test1() {
        return "hello";
    }

    @RequestMapping("/list")
    @ResponseBody
    public String list(@RequestParam List<String> hbs) {
        System.out.println(hbs);
        return "list";
    }

    @RequestMapping("/obj")
    @ResponseBody
    public String list1(@RequestBody User user ) {
        System.out.println(user);
        return "obj";
    }

    @RequestMapping("/delete/{id}/{name}")
    @ResponseBody
    public String list2( @PathVariable("id") int id,@PathVariable("name") String name ) {
        System.out.println(name);
        System.out.println(id);
        return "PathVariable";
    }

    @RequestMapping("/testCookie")
    public String test2(@CookieValue("JSESSIONID") String jsessionId) {
        System.out.println(jsessionId);
        return "hello";
    }

    @RequestMapping("/testHeader")
    public String test3(@RequestHeader("host") String host,@RequestHeader("cookie")String cookie) {
        System.out.println(host);
        System.out.println(cookie);
        return "hello";
    }

    //request 级别共共享域
    @RequestMapping("/request")
    public String  test4(Model model, ModelMap modelMap, Map<String,String> map, HttpServletRequest request) {
        model.addAttribute("model","modelData");
        modelMap.addAttribute("modelMap","modelMapData");
        map.put("map", "mapData");
        request.setAttribute("request", "requestData");
        return "hello";
    }
    @RequestMapping("/request1")
    public ModelAndView  test4() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("modelAndView","modelAndViewData");
        modelAndView.setViewName("hello");
        return modelAndView;
    }

    // session
    @RequestMapping("/session")
    public String  test5(HttpSession session) {
        session.setAttribute("session","sessionData");
        return "hello";
    }

    @Autowired
    private ServletContext servletContext;
    @RequestMapping("/servletContext")
    public String  test5() {
        servletContext.setAttribute("servletContext","servletContextData");
        return "hello";
    }

    // 转发
    @RequestMapping("/forward")
    public String  test6() {
        return "forward:/user/servletContext";
    }
    // 重定向
    @RequestMapping("/redirect")
    public String  test7() {
        return "redirect:/user/session";
    }
}
